package com.liser.material.design.ui.home;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import androidx.recyclerview.widget.RecyclerView;

import com.alibaba.android.vlayout.DelegateAdapter;
import com.alibaba.android.vlayout.LayoutHelper;
import com.liser.material.design.R;
import com.liser.material.design.net.model.GoodsPrintResult;

import java.util.ArrayList;
import java.util.List;

/**
 * @author Administrator
 * @date 2019/12/27 17:51
 */
public class GoodsLinearItemAdapter extends DelegateAdapter.Adapter<GoodsLinearItemAdapter.RecyclerViewItemHolder> {

    private Context context;

    private LayoutHelper mHelper;

    private List<GoodsPrintResult> data = new ArrayList<>();

    public GoodsLinearItemAdapter(Context context, LayoutHelper helper) {
        this.context = context;
        this.mHelper = helper;
    }

    @Override
    public LayoutHelper onCreateLayoutHelper() {
        return mHelper;
    }

    @Override
    public RecyclerViewItemHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(context).inflate(R.layout.adapter_item_goods, parent, false);
        return new RecyclerViewItemHolder(view);
    }

    @Override
    public void onBindViewHolder(RecyclerViewItemHolder holder, final int position) {
        GoodsPrintResult goodsPrintResult = data.get(position);
        holder.itemNoTV.setText(goodsPrintResult.getItemNo());
        holder.itemNameTV.setText(goodsPrintResult.getItemName());
        holder.itemSizeTV.setText(goodsPrintResult.getItemSize());
        holder.salePriceTV.setText(String.valueOf(goodsPrintResult.getSalePrice()));
        holder.unitNoTV.setText(goodsPrintResult.getUnitNo());
    }

    @Override
    public int getItemCount() {
        if (data == null) {
            return 0;
        }
        return data.size();
    }

    public List<GoodsPrintResult> getData() {
        return data;
    }

    public void addData(GoodsPrintResult result) {
        this.data.add(result);
        notifyDataSetChanged();
    }

    public void setData(List<GoodsPrintResult> data) {
        this.data = data;
        notifyDataSetChanged();
    }

    class RecyclerViewItemHolder extends RecyclerView.ViewHolder {

        TextView itemNoTV;

        TextView itemNameTV;

        TextView itemSizeTV;

        TextView salePriceTV;

        TextView unitNoTV;

        public RecyclerViewItemHolder(View itemView) {
            super(itemView);
            itemNoTV = itemView.findViewById(R.id.item_no);
            itemNameTV = itemView.findViewById(R.id.item_name);
            itemSizeTV = itemView.findViewById(R.id.item_size);
            salePriceTV = itemView.findViewById(R.id.sale_price);
            unitNoTV = itemView.findViewById(R.id.unit_no);
        }
    }
}
